<template>
  <div>
    <QueryFrame v-on:query="quickQuery" />
      <el-table :data="messageTable" ref="dynamicHeightTable" :max-height="tableMaxHeight-84">
        <el-table-column label="序号"  width="55" align="center">
          <template slot-scope="scope">
            {{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="messageTitle" label="名称" align="center"></el-table-column>
        <el-table-column prop="messageType" label="通知类型" align="center">
          <template slot-scope="scope">
            <dict-tag :options="MESSAGE_TYPE" :value="scope.row.messageType"/>
          </template>
        </el-table-column>
        <el-table-column prop="messageContent" label="内容" align="center"></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          align="center"
          :formatter="megRead"
        >
        </el-table-column>
        <el-table-column prop="createTime" align="center" label="时间">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link
              type="primary"
              class="opertion"
              :disabled="scope.row.status == '1'"
              @click="hadbeenRead(scope.row)"
              >已读</el-link
            >
            <el-link  v-hasPermi="['basic:message:link']" type="primary" :disabled="!scope.row.link" @click="toRoute(scope.row)">前往</el-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="messageTotal > 0"
        :total="messageTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="localMessageList"
      />
  </div>
</template>

<script>
import { messageReadDone, messageList } from "@/api/basic/center";
import QueryFrame from "../QueryFrame/index";
import tableMaxHeightRefresh from "@/mixins/tableMaxHeightRefresh";

const MESSAGE_TYPE = [
  {value: "0", label: '提醒'},
  {value: "1", label: '警告'},
  {value: "2", label: '超时预警'},
  {value: "3", label: '巡检提醒'},
  {value: "4", label: '保养提醒'},
  {value: "5", label: '报修提醒'},
  {value: "6", label: '维修提醒'},
  {value: "9", label: '系统通知'},
]

export default {
  props: {
    innerFormat: Function,
  },
  mixins:[tableMaxHeightRefresh],
  data() {
    return {
      // 通知数据总数
      messageTotal: 0,
      // 通知表
      messageTable: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: "",
        messageTitle: "",
      },
    };
  },
  computed: {
    MESSAGE_TYPE() {return MESSAGE_TYPE}
  },
  created() {
    this.localMessageList();
  },
  methods: {
    quickQuery(data) {
      this.queryParams.messageTitle = data;
      this.localMessageList();
    },
    localMessageList() {
      messageList(this.queryParams).then((res) => {
        if (res.code === 200 && res.rows.length > 0) {
          this.messageTable = res.rows;
          this.messageTotal = res.total;
        } else {
          this.messageTable = [];
        }
      });
    },
    toRoute(row) {
      this.$router.push({path: row.link, query: {messageCreateTime:row.createTime}})
    },
    hadbeenRead(row) {
      let messageId = row.messageId;
      messageReadDone({ messageId }).then((res) => {
        if (res.code === 200) {
          this.localMessageList();
        }
      });
    },
    // 通知数据转换
    megRead(row) {
      return this.innerFormat(row.status);
    },
  },
  components: {
    QueryFrame
  },
};
</script>

<style>
</style>
